//
//  ZYPageListView.m
//  KaKaWallet
//
//  Created by admin on 2016/10/31.
//  Copyright © 2016年 ibendi. All rights reserved.
//

#import "ZYPageListView.h"

@interface ZYPageListView()<UITableViewDataSource,UITableViewDelegate>

@property (strong,nonatomic) UITableView *tableview;
@property (strong,nonatomic) NSArray *dataArray;
@property (nonatomic,strong)NSTimer *myTimer;

@property (assign,nonatomic) NSInteger rowNum;

@end

@implementation ZYPageListView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
       
        self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) style:UITableViewStylePlain];
        self.tableview.delegate =self;
        self.tableview.dataSource =self;
        [self addSubview:self.tableview];
//        self.tableview.tableHeaderView = [UIView new];

        self.tableview.backgroundColor = [UIColor redColor];

        self.rowNum = 0;
    }
    return self;
}


- (void)stop {
    if (_myTimer) {
        [_myTimer invalidate];
    }
}
-(void)startWithdata:(NSArray *)array
{
    self.dataArray = array;
    [self.tableview reloadData];

    [self.tableview setContentOffset:CGPointMake(0, 0) animated:YES];
    _myTimer = [NSTimer scheduledTimerWithTimeInterval:1.1 target:self selector:@selector(doImageGoDisplay) userInfo:nil repeats:YES];
}
-(void)doImageGoDisplay
{
    [self.tableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.rowNum inSection:0]
                            atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
//    [self performSelector:@selector(reloadImages) withObject:nil afterDelay:0.3];
    self.rowNum ++;
    if (self.rowNum == self.dataArray.count) {
        self.rowNum = 0;
    }


}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.kkRowCell) {
        return self.kkRowCell(tableView, indexPath);
    }
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
//    cell.textLabel.text = self.dataArray[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.kkSelect) {
        self.kkSelect(tableView, indexPath);
    }
}

-(void)zyCellForRowAtIndexPath:(cellForRow)cellCompletion wihtDidSelectRowAtIndexPath:(didSelectRow)didCompletion
{
    if (self.kkRowCell) {
        self.kkRowCell = cellCompletion;
    }
    if (self.kkSelect) {
        self.kkSelect = didCompletion;
    }
}
@end
